/**
 * 项目版本获取接口测试用例
 *
 * Created by raquelmao on 2020/08/25.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('../../common');
const Administrator = require('../../../models/administrator');
const uri = require('../uri');
const benchmark = require('../../../benchmark/analyse-files/logic_bombs');

module.exports = describe('/GET project-versions/:id/functions/overview.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  let rootDirectory;
  let fileId;
  const newUserIds = [];
  const extensions = benchmark.config.languages.c.suffixes
    .concat(benchmark.config.headers.suffixes);
  const allFiles = [];
  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}/functions/overview.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        return project.createRandomVersion();
      })
      .then((res) => {
        projectVersion = res;
        rootDirectory = projectVersion.rootDirectory;
        return projectVersion.updateVersionFiles(benchmark.files, benchmark.config);
      })
      .then(() => rootDirectory.analyse())
      .then(() => rootDirectory.getTree(extensions))
      .then((tree) => {
        common.should.be.fileTreeMessage(tree);
        common.should.be.equal.tree(tree.tree, benchmark.result.tree);
        const rootArray = [tree.tree];
        while (rootArray.length > 0) {
          const root = rootArray.pop();
          if (root.children) {
            root.children.forEach((child) => {
              rootArray.push(child);
            });
          }
          allFiles.push(root);
        }
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常请求
  it('it should GET a functions list overview', function (done) {
    project.creator.login()
      .then(() => {
        chai.request(uri)
          .get(url())
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.functionsOverviewMessage(res.body);
            res.body.functions.length.should.be.gt(0);
            fileId = res.body.functions[0].fileId;
            done();
          });
      })
      .catch(done);
  });

  // 不可获取不存在的项目版本
  it('it should fail to get a functions list overview when the version is not exists', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .get(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可获取他人的项目版本
  it('it should fail to get a functions list overview when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((newTester) => {
        newUser = newTester;
        newUserIds.push(newTester.userId);
        return newTester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .get(url())
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // get functions of file id
  it('get functions by file id', function (done) {
    project.creator.login()
      .then(() => {
        chai.request(uri)
          .get(`${url()}?fileId=${fileId}`)
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.functionsOverviewMessage(res.body);
            res.body.functions.length.should.be.gt(0);
            res.body.functions.forEach((item) => item.fileId.should.be.eq(fileId));
            done();
          });
      })
      .catch(done);
  });
});
